package com.qls.consumer.store.config;


import com.alibaba.fastjson.JSONObject;
import com.qls.ali.api.AliRemoteService;
import com.qls.consumer.store.service.OpTaskStatusService;
import com.qls.core.dto.BatchPushRatesBody;
import com.qls.core.dto.PushHotelBody;
import com.qls.core.dto.PushRatesBody;
import com.qls.core.dto.PushRpBody;
import com.qls.mt.api.MtRemoteService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Objects;


/**
 * @author doyoung
 */
@Component
@Slf4j
public class ReceiveHandler {

    private final Integer storeId = 3;

    @Autowired
    private MtRemoteService mtRemoteService;

    @Autowired
    private AliRemoteService aliRemoteService;

    @Autowired
    private OpTaskStatusService opTaskStatusService;

    @RabbitListener(queues = "3_rates", containerFactory = "rates")
    public void rates(List<Message> list) {
        List<PushRatesBody> pushRatesBodies = new ArrayList<>();
        for (Message msg : list) {
            byte[] bytes = msg.getBody();
            PushRatesBody pushRatesBody = JSONObject.parseObject(bytes, PushRatesBody.class);
            Integer taskId = pushRatesBody.getTaskId();
            Long rpCode = pushRatesBody.getRpCode();
            this.add(taskId, rpCode);
            this.over(rpCode, taskId);
            if (rpCode.equals(-1L)) {
                continue;
            }
            pushRatesBodies.add(pushRatesBody);
        }
        BatchPushRatesBody batchPushRatesBody = new BatchPushRatesBody();
        batchPushRatesBody.setPushRatesBodies(pushRatesBodies);
        batchPushRatesBody.setStoreId(storeId);
        aliRemoteService.pushRates(batchPushRatesBody);
    }

    @RabbitListener(queues = "3_rp", containerFactory = "rp")
    public void rp(PushRpBody msg) {
        Integer taskId = msg.getTaskId();
        this.add(taskId, msg.getRpCode());
        this.over(msg.getRpCode(), taskId);
        if (msg.getRpCode().equals(-1L)) {
            return;
        }
        msg.setStoreId(storeId);
        aliRemoteService.pushRp(msg);
    }

    @RabbitListener(queues = "3_hotel", containerFactory = "hotel")
    public void hotel(PushHotelBody msg) {
        Integer taskId = msg.getTaskId();
        Integer type = msg.getType();
        Long hotelId = msg.getHotelId();
        this.add(taskId, msg.getHotelId());
        this.over(msg.getHotelId(), taskId);
        msg.setStoreId(storeId);
        if (hotelId.equals(-1L)) {
            return;
        }
        long start = System.currentTimeMillis();
        try {
            if (type.equals(1)) {
                mtRemoteService.hotelAll(msg);
                log.info("店铺id：{}，酒店id：{}，拉取酒店，类型：{}, 耗时：{}ms", storeId, hotelId, type, (System.currentTimeMillis() - start));
            }
            long startPush = System.currentTimeMillis();
            aliRemoteService.pushHotel(msg);
            log.info("店铺id：{}，酒店id：{}，推送酒店，类型：{}, 耗时：{}ms", storeId, hotelId, type, (System.currentTimeMillis() - startPush));
        } catch (Exception e) {
            log.warn("店铺id：{}，酒店id：{}，推送酒店，推送类型：{}，发生异常：{}", storeId, hotelId, type, e.getMessage());
        }
    }

    /**
     * 增加任务数量
     *
     * @param taskId Integer
     */
    private void add(Integer taskId, Long code) {
        if (Objects.isNull(taskId) || code.equals(-1L)) {
            return;
        }
        opTaskStatusService.completedAdd(taskId, 1);
    }

    /**
     * 结束任务
     *
     * @param code   Long
     * @param taskId Integer
     */
    private void over(Long code, Integer taskId) {
        if (Objects.nonNull(taskId) && code.equals(-1L)) {
            opTaskStatusService.updateByTaskCode(taskId, new Date());
        }
    }

}
